<?php
require ('core/shop.php');
quanli_require('framework/wx/wxHelp.php');
quanli_require('module/base/bill/billCost.class.php');
quanli_require('module/base/bill/billTemp.class.php');
quanli_require('module/base/bill/billSale.class.php');
quanli_require('module/base/system/sysUser.class.php');

if(isset($_REQUEST['ajaxFlg'])){

    switch ($_REQUEST['ajaxFlg']){
        case 1:
            costDtl();
            break;
        case 2:
            tempDtl();
            break;
        case 3:
            saleDtl();
            break;
        case 4:
            costConfirm();
            break;
        case 5:
            tempClaim();
            break;
        case 6:
            tempFinish();
        case 7:
            tempReceipt();
            break;
        case 8:
            saleFinish();
            break;
    }
}

function costDtl()
{

    $user = wxHelp::getUser();
    if(!$user) {
        Header("Location: /wap/login.html");
    }
    $costInfo = null;
    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billCost = new billCost( GS()->DBCA );
        $param = array( 'costCode' => $_REQUEST['code'] );
        $result = $billCost->searchBillCost($param);
        if ($result) {
            $costInfo = $result[0];
            $costInfo->createTime = date('Y-m-d H:i',strtotime($costInfo->createTime));
            $costInfo->statusName = wxhelp::$costStatus[$costInfo->costStatus];
        }
        $costInfo->dtl = $billCost->searchCostDtl($_REQUEST['code']);
    }
    GS()->smarty->assign('costInfo', $costInfo);
    GS()->smarty->assign('user', $user);
    GS()->smarty->display('order_cost_dtl.tpl');
}

function tempDtl()
{
    $user = wxHelp::getUser();
    if(!$user) {
        Header("Location: /wap/login.html");
    }

    $tempInfo = null; $errMsg = '';
    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billTemp = new billTemp( GS()->DBCA );
        $param = array( 'tempCode' => $_REQUEST['code'] );
        $result = $billTemp->searchBillTemp($param);
        if ($result) {
            $tempInfo = $result[0];
            $tempInfo->createTime = date('Y-m-d H:i',strtotime($tempInfo->createTime));
            $tempInfo->statusName = wxhelp::$tempStatus[$tempInfo->tempStatus];
        }
        $resGoods = $billTemp->searchTempDtl($_REQUEST['code']);
        $resAnnex = $billTemp->searchAnnex($_REQUEST['code']);
        if($resAnnex){
            foreach ($resGoods as &$goods) {
                $temp = null;
                foreach ($resAnnex as $annex) {
                    if($goods->goodsId == $annex->goodsId){
                        $annex->path = G_IMAGE_URL.'goods/wx/'.$annex->fileName;
                        $temp[] = $annex;
                    }
                }
                $goods->annex = $temp;
            }
        }
        $tempInfo->dtl = $resGoods;

        if($user['type'] == 1 && $tempInfo->producer && $tempInfo->producer != $user['id']){
            $errMsg = '该小样订单已被认领或不存在';
        }
        if($user['type'] == 0 && $tempInfo->companyId != $user['id']){
            $errMsg = '未找到该小样订单数据';
        }

    }

    $signPackage = wxHelp::getSDK();
    GS()->smarty->assign('signPackage', $signPackage);

    GS()->smarty->assign('user', $user);
    GS()->smarty->assign('tempInfo', $tempInfo);
    GS()->smarty->assign('errMsg', $errMsg);
    GS()->smarty->display('order_temp_dtl.tpl');
}

function saleDtl(){
    $user = wxHelp::getUser();
    if(!$user) {
        Header("Location: /wap/login.html");
    }

    $costInfo = null;
    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billSale = new billSale( GS()->DBCA );
        $param = array( 'saleCode' => $_REQUEST['code'] );
        $result = $billSale->searchList($param);
        if ($result) {
            $saleInfo = $result[0];
            $saleInfo->createTime = date('Y-m-d H:i',strtotime($costInfo->createTime));
            $saleInfo->statusName = wxhelp::$saleStatus[$saleInfo->saleStatus];
        }
        $resGoods = $billSale->searchDtl($_REQUEST['code']);
        $resAnnex = $billSale->searchAnnex($_REQUEST['code']);
        if($resAnnex){
            foreach ($resGoods as &$goods) {
                $temp = null;
                foreach ($resAnnex as $annex) {
                    if($goods->goodsId == $annex->goodsId){
                        $annex->path = G_IMAGE_URL.'goods/wx/'.$annex->fileName;
                        $temp[] = $annex;
                    }
                }
                $goods->annex = $temp;
            }
        }
        $saleInfo->dtl = $resGoods;
    }

    $signPackage = wxHelp::getSDK();
    GS()->smarty->assign('signPackage', $signPackage);
    GS()->smarty->assign('saleInfo', $saleInfo);
    GS()->smarty->assign('user', $user);
    GS()->smarty->display('order_sale_dtl.tpl');
}

function costConfirm()
{
    $user = wxHelp::getUser();
    if(!$user) {
        returnJsonData('请先绑定用户', 400);
    }

    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billCost = new billCost( GS()->DBCA );
        $param = array( 'costCode' => $_REQUEST['code'], 'status' => 2 );
        $result = $billCost->changeCostStatus($param);
        if($result && $result['code'] == 200)
            returnJsonData('确认成功', 200);
    }
    returnJsonData('确认失败', 400);
}

function tempClaim(){
    $user = wxHelp::getUser();
    if(!$user) {
        returnJsonData('请先绑定用户', 400);
    }

    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billTemp = new billTemp( GS()->DBCA );
        $param = array( 'tempCode' => $_REQUEST['code'], 'producer' => $user['id'], 'status' => 2);
        $res = $billTemp->changeStatus($param);

        try{
            quanli_include('module/base/company/companyMaster.class.php');
            $company = new companyMaster();
            $arg = array('company_id' => $res['data']->companyId);
            $compay_result = $company->search( $arg );
            if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
                quanli_include('framework/wx/wxHelp.php');
                $params = array(
                    'openId' => $compay_result[0]['wx_openid'],
                    'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                    'title' => '小样订单提醒',
                    'code' => $_REQUEST['code'],
                    'date' => date('Y-m-d'),
                    'remark' => '您的小样订单已经开始制作，点击查看详情'
                );
                wxHelp::wxMessage($params);
                /*推送给对应客户的销售员微信账号*/
                global $G_SHOP;
                $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
                $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
                $temp_user = $user_master->searchUserInfo($param);
                if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                    $params = array(
                        'openId' => $temp_user[0]['wx_openid'],
                        'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                        'title' => '小样订单提醒',
                        'code' => $_REQUEST['code'],
                        'date' => date('Y-m-d'),
                        'remark' => '大客户' . $compay_result[0]['company_name'] . '的小样订单已经开始制作，点击查看详情'
                    );
                    wxHelp::wxMessage($params);
                }
            }
        }catch (Exception $e){ elog($e->getMessage()); }

        if($res && $res['code'] == 200)
            returnJsonData('认领成功', 200);
    }
    returnJsonData('认领失败', 400);
}

function tempFinish (){
    $user = wxHelp::getUser();
    if(!$user) {
        returnJsonData('请先绑定用户', 400);
    }

    if(!isset($_REQUEST['code']) || empty($_REQUEST['code'])) {
        returnJsonData('参数错误', 400);
    }

    if(!isset($_REQUEST['files'])) {
        returnJsonData('请先上传图片', 400);
    }

    $billTemp = new billTemp( GS()->DBCA );

    //处理文件
    foreach ( $_REQUEST['files'] as $goodsId => &$item ) {
        foreach ( $item as $num => $serverId){
            if($serverId){
                //下载文件
                $fileName = $serverId .'.jpg';
                $imgPath = G_IMAGE_SAVE_ROOT . 'goods/wx/';
                wxHelp::downLoad($imgPath, $fileName, $serverId);
            }
        }
    }

    $param = array( 'tempCode' => $_REQUEST['code'], 'status' => 3, 'files' => $_REQUEST['files']);
    $res = $billTemp->changeStatus($param);

    try{
        quanli_include('module/base/company/companyMaster.class.php');
        $company = new companyMaster();
        $arg = array('company_id' => $res['data']->companyId);
        $compay_result = $company->search( $arg );
        if($compay_result && isset($compay_result[0]) && !empty($compay_result[0]['wx_openid'])){
            quanli_include('framework/wx/wxHelp.php');
            $params = array(
                'openId' => $compay_result[0]['wx_openid'],
                'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                'title' => '小样订单提醒',
                'code' => $_REQUEST['code'],
                'date' => date('Y-m-d'),
                'remark' => '您的小样订单已经制作完成，点击查看详情'
            );
            wxHelp::wxMessage($params);
            /*推送给对应客户的销售员微信账号*/
            global $G_SHOP;
            $param = array('userID' => $compay_result[0]['userID'], 'useFlg' => 1);
            $user_master = new sysUser($G_SHOP->DBCA);//实例用户类
            $temp_user = $user_master->searchUserInfo($param);
            if ($temp_user[0]['isSalesman'] == '1' && $temp_user[0]['wx_openid'] != ''){
                $params = array(
                    'openId' => $temp_user[0]['wx_openid'],
                    'url' => G_SHOP_CDN . '/wap/orderDtl.html?ajaxFlg=2&code='.$_REQUEST['code'],
                    'title' => '小样订单提醒',
                    'code' => $_REQUEST['code'],
                    'date' => date('Y-m-d'),
                    'remark' => '大客户' . $compay_result[0]['company_name'] . '的小样订单已经制作完成，点击查看详情'
                );
                wxHelp::wxMessage($params);
            }
        }
    }catch (Exception $e){ elog($e->getMessage()); }


    if($res && $res['code'] == 200)
        returnJsonData('操作成功', 200);

    returnJsonData('操作失败', 400);
}

function tempReceipt(){
    $user = wxHelp::getUser();
    if(!$user) {
        returnJsonData('请先绑定用户', 400);
    }

    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billTemp = new billTemp( GS()->DBCA );
        $param = array( 'tempCode' => $_REQUEST['code'], 'status' => 5);
        $result = $billTemp->changeStatus($param);
        if($result && $result['code'] == 200)
            returnJsonData('收货成功', 200);
    }
    returnJsonData('收货失败', 400);
}

function saleFinish(){
    $user = wxHelp::getUser();
    if(!$user) {
        returnJsonData('请先绑定用户', 400);
    }

    if(!isset($_REQUEST['files'])) {
        returnJsonData('请先上传图片', 400);
    }

    if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
        $billSale = new billSale( GS()->DBCA );
        //处理文件
        foreach ( $_REQUEST['files'] as $goodsId => &$item ) {
            foreach ( $item as $num => $serverId){
                if($serverId){
                    //下载文件
                    $fileName = $serverId .'.jpg';
                    $imgPath = G_IMAGE_SAVE_ROOT . 'goods/wx/';
                    wxHelp::downLoad($imgPath, $fileName, $serverId);
                }
            }
        }
        $param = array( 'saleCode' => $_REQUEST['code'], 'status' => 4,'files' => $_REQUEST['files']);
        $result = $billSale->changeStatus($param);
        if($result && $result['code'] == 200)
            returnJsonData('收货成功', 200);
    }
    returnJsonData('收货失败', 400);
}